﻿// (c) Copyright slimCODE Software Inc. - www.slimcode.com
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SlimCode.Windows.Forms.Mobile;
using SlimCode.Windows.Controls.Mobile;

namespace SlimCode.SlimPasswords
{
  internal partial class SelectStoreScreen : UserControl, IScreen
  {
    private IScreenHost m_host; // = null

    public SelectStoreScreen()
    {
      InitializeComponent();

      browseStoreButton.Item.Image = ImageCache.GetFolderImage( browseStoreButton.ClientRectangle.Height - 10 );
    }

    public string Password
    {
      get { return passwordTextBox.Text; }
    }

    public string StoreFilename
    {
      get { return storeFilenameTextBox.Text; }
      set 
      { 
        storeFilenameTextBox.Text = value ?? string.Empty;
        this.storeFilenameTextBox_TextChanged( storeFilenameTextBox, EventArgs.Empty );
      }
    }

    private void cancelMenuItem_Click( object sender, EventArgs e )
    {
      if( m_host != null )
      {
        m_host.HideScreen( DialogResult.Cancel );
      }
    }

    private void loadMenuItem_Click( object sender, EventArgs e )
    {
      if( m_host != null )
      {
        m_host.HideScreen( DialogResult.OK );
      }
    }

    private bool LoadOrCreatePasswordStore()
    {
      bool create = false;

      try
      {
        create = !System.IO.File.Exists( this.StoreFilename );

        if( create )
        {
          if( MessageBox.Show(
            "The specified filename does not exist. Do you want to create a new password store file?",
            "Confirm",
            MessageBoxButtons.YesNo,
            MessageBoxIcon.Question,
            MessageBoxDefaultButton.Button1 ) != DialogResult.Yes )
          {
            return false;
          }
        }
      }
      catch
      {
        MessageBox.Show(
          "The specified filename is invalid.",
          "Error",
          MessageBoxButtons.OK,
          MessageBoxIcon.Hand,
          MessageBoxDefaultButton.Button1 );

        return false;
      }

      if( create )
      {
        PasswordStore store = new PasswordStore( storeFilenameTextBox.Text, passwordTextBox.Text );

        if( Program.SaveStore( store, m_host ) )
        {
          Program.ActiveStore = store;
        }
        else
        {
          return false;
        }
      }
      else
      {
        if( !Program.LoadStore( storeFilenameTextBox.Text, passwordTextBox.Text ) )
        {
          return false;
        }
      }

      return true;
    }

    private void browseStoreButton_Click( object sender, EventArgs e )
    {
      if( m_host != null )
      {
        m_host.ShowScreen( new BrowseFileScreen() );
      }
    }

    private void showPasswordCheckBox_CheckStateChanged( object sender, EventArgs e )
    {
      passwordTextBox.PasswordChar = ( showPasswordCheckBox.Checked ? '\0' : '*' );
    }

    private void passwordTextBox_KeyPress( object sender, KeyPressEventArgs e )
    {
      if( e.KeyChar == '\r' )
      {
        e.Handled = true;

        if( m_host != null )
        {
          m_host.HideScreen( DialogResult.OK );
        }
      }
    }

    private void storeFilenameTextBox_TextChanged( object sender, EventArgs e )
    {
      loadMenuItem.Enabled = ( ( storeFilenameTextBox.Text.Length > 0 ) && ( passwordTextBox.Text.Length > 0 ) );
      this.UpdateLoadMenuText();
    }

    private void UpdateLoadMenuText()
    {
      try
      {
        loadMenuItem.Text = System.IO.File.Exists( storeFilenameTextBox.Text )
          ? "Load" : "Create";
      }
      catch
      {
        loadMenuItem.Text = "Load";
      }
    }

    private void passwordTextBox_TextChanged( object sender, EventArgs e )
    {      
      loadMenuItem.Enabled = ( ( storeFilenameTextBox.Text.Length > 0 ) && ( passwordTextBox.Text.Length > 0 ) );
    }

    private void AnyControl_GotFocus( object sender, EventArgs e )
    {
      if( m_host != null )
      {
        Program.ToggleInputPanelIf( m_host.MainForm.MainInputPanel, ( sender is TextBox ) );
      }
    }

    #region IScreen Members

    public Control Control
    {
      get { return this; }
    }

    public Control DefaultFocus
    {
      get 
      {
        return ( storeFilenameTextBox.TextLength == 0 ) ? storeFilenameTextBox : passwordTextBox; 
      }
    }

    public MainMenu Menu
    {
      get { return mainMenu; }
    }

    public string Title
    {
      get { return "slimPASSWORDS"; }
    }

    public bool Hiding( DialogResult result )
    {
      if( result == DialogResult.OK )
      {
        if( storeFilenameTextBox.TextLength == 0 )
        {
          MessageBox.Show(
            "You must enter the name of the password store file you wish to load or create.",
            "Empty filename",
            MessageBoxButtons.OK,
            MessageBoxIcon.Exclamation,
            MessageBoxDefaultButton.Button1 );

          return false;
        }

        if( passwordTextBox.TextLength == 0 )
        {
          MessageBox.Show(
            "You must enter a password.",
            "Empty password",
            MessageBoxButtons.OK,
            MessageBoxIcon.Exclamation,
            MessageBoxDefaultButton.Button1 );

          return false;
        }

        if( !this.LoadOrCreatePasswordStore() )
        {
          return false;
        }
      }

      return true;
    }

    public void HidingChild( IScreen child, DialogResult result )
    {
      if( result == DialogResult.OK )
      {
        BrowseFileScreen browseScreen = child as BrowseFileScreen;

        if( browseScreen != null )
        {
          storeFilenameTextBox.Text = browseScreen.SelectedFile;
        }
      }
    }

    public void Showing( IScreenHost host )
    {
      m_host = host;

      /*
      if( this.StoreFilename.Length > 0 )
      {
        passwordTextBox.Focus();
      }
      else
      {
        storeFilenameTextBox.Focus();
      }
      */
    }

    #endregion
  }
}
